<template>
  <el-container direction="vertical">
  <el-table
          :data="tableDataFilter"
          stripe
          border
          style="width: 100%">
    <el-table-column
            prop="username"
            label="用户名"
            width="180">
    </el-table-column>
    <el-table-column
            prop="password"
            label="密码">
    </el-table-column>
    <el-table-column
            prop="id"
            label="ID"
            width="180">
    </el-table-column>
    <el-table-column
            fixed="right"
            width="300">
            <template slot="header" slot-scope="scope">
            <el-input
              v-model="dataFilter"
              placeholder="输入搜索关键字"/>
            </template>
      <template slot-scope="scope">
        <el-button @click="handleClick(scope.row)" type="text" size="small">编辑</el-button>
        <el-button @click="deleteClick(scope.row)" type="text" size="small">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
    <el-button @click="testClick" type="primary">编辑</el-button>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      dataFilter: '',
      tableData: null
    }
  },
  methods: {
    testClick(){
      this.$axios.post('user',{
          username: 'admin',
          password: 'password',
          role_id: 1
          // idCard: ''
        }).then(response => {
        if (response.data.code === 0){
          this.tableData.push(response.data.returnObject)
        }
        else {
          this.$message.error(response.data.message)
        }
      }).catch(error => {
        console.log(error)
      })
    },
    handleClick(row) {
      console.log(row);
    },
    deleteClick(row){
      console.log(row)
      this.$axios.delete('user/' + row.id).then(response => {
        if (response.data.code === 0){
          this.tableData.splice(0, 1)
        }
      }).catch(error => {
        console.log(error)
      })
    }
  },
  computed: {
    tableDataFilter(){
      return this.dataFilter.length ? this.tableData.filter(item => item.username.includes(this.dataFilter)) : this.tableData
    }
  },
  created() {
    this.$axios.get('user').then(response => {
      console.log(response.data)
      if (response.data.code === 0){
        this.tableData = response.data.returnObject
      }
    }).catch(error => {
      console.log(error)
    })
  }
}
</script>
